package com.hd.trans.ui.listener;

import android.content.Context;
import android.util.Log;
import android.view.OrientationEventListener;

import androidx.activity.ComponentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;

public class ScreenOrientationListener extends OrientationEventListener {
    private static final String TAG = ScreenOrientationListener.class.getSimpleName();
    private int mOrientation;

    public boolean isSpecialOrientation() {
        return isSpecialOrientation;
    }

    public void setSpecialOrientation(boolean specialOrientation) {
        isSpecialOrientation = specialOrientation;
    }

    private boolean isSpecialOrientation = true;
    private OnOrientationChangedListener mOnOrientationChangedListener;

    public ScreenOrientationListener(ComponentActivity activity, OnOrientationChangedListener listener) {
        this(activity);
        this.mOnOrientationChangedListener = listener;
    }
    public ScreenOrientationListener(ComponentActivity activity) {
        super(activity);

        Lifecycle lifecycle = activity.getLifecycle();
        if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
            enable();
        }
        lifecycle.addObserver((LifecycleEventObserver) (source, event) -> {
            switch (event){
                case ON_RESUME:
                    enable();
                    break;
                case ON_PAUSE:
                    disable();
                    break;
            }
        });
    }

    public void setOnOrientationChangedListener(OnOrientationChangedListener listener) {
        this.mOnOrientationChangedListener = listener;
    }

    @Deprecated
    public void onResume(){
        /*if (canDetectOrientation()) {
            enable();
        } else {
            disable();
        }*/
    }

    @Deprecated
    public void onPause(){
        //disable();
    }

    @Override
    public void onOrientationChanged(int orientation) {
        if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
            if(mOrientation != 0){
                mOrientation = 0;
                if (null != mOnOrientationChangedListener) {
                    mOnOrientationChangedListener.onOrientationChanged(mOrientation);
                }
            }
            return; // 手机平放时，检测不到有效的角度
        }
        if(isSpecialOrientation){
            //只检测是否有四个角度的改变
            if (orientation > 345 || orientation < 15) { //0度
                orientation = 0;
            } else if (orientation > 75 && orientation < 105) { //90度
                orientation = 90;
            } else if (orientation > 165 && orientation < 195) { //180度
                orientation = 180;
            } else if (orientation > 255 && orientation < 285) { //270度
                orientation = 270;
            } else {
                orientation = 0;
            }
        }

        if(mOrientation != orientation){
            mOrientation = orientation;
            if (null != mOnOrientationChangedListener) {
                mOnOrientationChangedListener.onOrientationChanged(mOrientation);
                //Log.d(TAG, "ScreenOrientationListener onOrientationChanged orientation=" + mOrientation);
            }
        }
    }

    public interface OnOrientationChangedListener {
        void onOrientationChanged(int orientation);
    }
}
